package com.weight.system.type.web;

import com.weight.system.type.PO.TypeInfo;
import com.weight.system.type.VO.TypeInfoVO;
import com.weight.system.type.service.TypeInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 类别常量表
 */
@RestController
@Slf4j
@RequestMapping("/type/typeInfo")
public class TypeInfoController {

	@Autowired
	private TypeInfoService typeInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询类别常量表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForTypeInfoList",method=RequestMethod.GET)
	public ModelAndView queryForTypeInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("system/type/queryForTypeInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询类别常量表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForTypeInfoListDG",method=RequestMethod.GET)
	public String queryForTypeInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String typeName = request.getParameter("typeName");
			String typeGroup = request.getParameter("typeGroup");
			String typeLevel = request.getParameter("typeLevel");
			String typeSort = request.getParameter("typeSort");
			String typeStatus = request.getParameter("typeStatus");
			String typeDesc = request.getParameter("typeDesc");

			Map<Object, Object> map =new HashMap<>();
			map.put("typeName", typeName);
			map.put("typeGroup", typeGroup);
			map.put("typeLevel", typeLevel);
			map.put("typeSort", typeSort);
			map.put("typeStatus", typeStatus);
			map.put("typeDesc", typeDesc);

			
			//2、查询类别常量表列表
			List<TypeInfo> typeInfoList = typeInfoService.findTypeInfoByPage(page,rows,map);
			log.info("typeInfoList={}",typeInfoList);
			List<TypeInfoVO> typeInfoVOList = typeInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<TypeInfoVO> result = new PageResult<TypeInfoVO>(typeInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param typeInfoVO
	 * @param typeInfo
	 * @return
	 */
	public void changeVOToPO(TypeInfoVO typeInfoVO,TypeInfo typeInfo){
			BeanUtils.copyProperties(typeInfoVO,typeInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param typeInfoVO
	 * @param typeInfo
	 * @return
	 */
	public void changePoToVO(TypeInfoVO typeInfoVO,TypeInfo typeInfo){
			BeanUtils.copyProperties(typeInfo,typeInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param typeInfo
	 * @return
	 */
	public TypeInfoVO changePoToVO(TypeInfo typeInfo){
			TypeInfoVO typeInfoVO=new TypeInfoVO();
			BeanUtils.copyProperties(typeInfo,typeInfoVO);
			

			return typeInfoVO;
	}

	/**
	 * 3.查询类别常量表--详情页
	 * @param typeCode
	 * @return
	 */
	@RequestMapping("/queryForTypeInfoDetail")
	public ModelAndView queryForTypeInfoDetail(@RequestParam(value="typeCode",required=false) String typeCode){
			//1.校验id
			CommonUtils.validateExistID(typeCode);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("typeCode", typeCode);
			log.info("查询类别常量表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			TypeInfo typeInfo = typeInfoService.findByTypeInfoId(map);
			
			TypeInfoVO typeInfoVO = changePoToVO(typeInfo);
			//3、返回前台
			log.info("查询类别常量表查询成功：typeInfo={}",typeInfo);
			modelAndView.setViewName("system/type/queryForTypeInfoDetail");
			modelAndView.addObject("typeInfoVO",typeInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改类别常量表操作
	 * @param typeCode
	 * @return
	 */
	@RequestMapping(value="/addTypeInfo")
	public ModelAndView addTypeInfo(@RequestParam(value="typeCode",required=false) String typeCode){
			//1、创建返回对象
			TypeInfoVO typeInfoVO = new TypeInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(typeCode)) {
				Map<String, String> map=new HashMap<>();
				map.put("typeCode", typeCode);
				log.info("修改操作，查询ID：typeCode={}",typeCode);
				TypeInfo typeInfo = typeInfoService.findByTypeInfoId(map);
				this.changePoToVO(typeInfoVO,typeInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("system/type/addTypeInfo");
			modelAndView.addObject("typeInfoVO",typeInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改类别常量表操作
	 * @param typeInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveTypeInfo")
	public JsonResultUtil saveTypeInfo(@Valid TypeInfoVO typeInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,typeInfoVO);
			
			//2.获取id
			String typeCode=typeInfoVO.getTypeCode();
			TypeInfo typeInfo=new TypeInfo();
			BeanUtils.copyProperties(typeInfoVO,typeInfo);
			this.changeVOToPO(typeInfoVO,typeInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(typeCode)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("typeCode", typeCode);
				log.info("修改操作，查询ID：typeCode={}",typeCode);
				//查询是否存在
				typeInfoService.findByTypeInfoId(map);
				try{
					typeInfo.setUpdateBy("000");
					typeInfoService.updateTypeInfo(typeInfo);
					log.info("更新操作成功：typeInfo={}",typeInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					typeInfo.setTypeCode(CommonUtils.getUUId());
					typeInfo.setCreateBy("000");
					typeInfo.setUpdateBy("000");
					typeInfoService.insertTypeInfo(typeInfo);
					log.info("添加操作成功：typeInfo={}",typeInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除类别常量表操作
	 * @param typeCode
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByTypeInfoId")
	public JsonResultUtil deleteByTypeInfoId(@RequestParam(value="typeCode",required=false) String typeCode){
			//1.校验id
			CommonUtils.validateExistID(typeCode);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("typeCode", typeCode);
			typeInfoService.findByTypeInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除类别常量表入参，map={}",map);
			
			try{
				typeInfoService.upDelByTypeInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除类别常量表操作---批量删除
	 * @param typeCodeList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByTypeInfoIdBatch")
	public JsonResultUtil deleteByTypeInfoIdBatch(@RequestParam(value="typeCodeList",required=false) List typeCodeList){
			//1.校验id
			CommonUtils.validateExistIDList(typeCodeList);
			
			//2.校验数据库
			typeInfoService.findByTypeInfoIdList(typeCodeList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("typeCodeList", typeCodeList);
			map.put("updateBy","000");
			log.info("批量删除类别常量表操作入参，map={}",map);
			
			try{
				typeInfoService.upDelByTypeInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}